using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Autorun.Server.Controls
{
	public partial class MiniStats : PaneCaption
	{
		#region "Private members"
		private LinearGradientBrush brBack;
		private Pen pUsage = new Pen(Color.DarkOrchid);
		private Pen pLines = new Pen(Color.BlueViolet);
		#endregion

		#region "Constructors and Form's Events Handlers"
		public MiniStats()
		{
			InitializeComponent();
			brBack = new LinearGradientBrush(DisplayRectangle, Color.Indigo, Color.MediumVioletRed, LinearGradientMode.Horizontal);
			lblTime.Text = DateTime.Now.ToString("HH:mm:ss");
			lblT50.Text = DateTime.Now.AddSeconds(-120 * MonitorManagement.Instance.Resolution).ToString("HH:mm:ss");
			lblT00.Text = DateTime.Now.AddSeconds(-240 * MonitorManagement.Instance.Resolution).ToString("HH:mm:ss");
		}
		#endregion

		#region "Properties"
		internal bool IsActive
		{
			get { return myTimer.Enabled; }
			set { myTimer.Enabled = value; }
		}
		#endregion

		#region "Controls Events Handlers"
		private void pbChart_Paint(object sender, PaintEventArgs e)
		{
			Bitmap b;
			Graphics g;
			int[,] data = new int[3, 480];


			b = new Bitmap(pbChart.Width, pbChart.Height, e.Graphics);
			g = Graphics.FromImage(b);

			g.FillRectangle(brBack, 0, 0, 240, 50);
			g.DrawLine(pLines, 0, 12, 240, 12);
			g.DrawLine(pLines, 0, 25, 240, 25);
			g.DrawLine(pLines, 0, 37, 240, 37);
			g.DrawLine(pLines, 60, 0, 60, 50);
			g.DrawLine(pLines, 120, 0, 120, 50);
			g.DrawLine(pLines, 180, 0, 180, 50);

			// === Draws the background ============================================
			e.Graphics.DrawImageUnscaled(b, 0, 0);
			if (myTimer.Enabled == false) return;

			data = MonitorManagement.Instance.Data;
			for (int col = 0; col < 240; col++)
			{
				g.DrawLine(pUsage, 240 - col, 50, 240 - col, 50 - (data[0, col] / 2));
			}

			e.Graphics.DrawImageUnscaled(b, 0, 0);
		}

		private void myTimer_Tick(object sender, EventArgs e)
		{
			lblTime.Text = DateTime.Now.ToString("HH:mm:ss");
			lblT50.Text = DateTime.Now.AddSeconds(-120 * MonitorManagement.Instance.Resolution).ToString("HH:mm:ss");
			lblT00.Text = DateTime.Now.AddSeconds(-240 * MonitorManagement.Instance.Resolution).ToString("HH:mm:ss");
			pbChart.Invalidate();
		}
		#endregion
	}
}
